package com.tanhua.manager.controller;

import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.vo.UserFreezeVo;
import com.tanhua.manager.service.UserFreezeService;
import com.tanhua.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 阿鹏
 * @date 2021/2/18 22:50
 */
@RestController
@RequestMapping("/manage")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserFreezeService userFreezeService;

    @GetMapping("/users")
    public ResponseEntity findUserList(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize){
        return userService.findUserList(page, pagesize);
    }

    @GetMapping("users/{id}")
    public ResponseEntity findUserInfo(@PathVariable("id") Long userId) {
        return userService.findUserInfo(userId);
    }

    @GetMapping("messages")
    public ResponseEntity findPublishMessages(@RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "10") int pagesize,
                                              @RequestParam(required = false) Long uid,
                                              @RequestParam(required = false) String state) {
        return userService.findPublishMessages(page, pagesize, uid, state);
    }

    @GetMapping("messages/{id}")
    public ResponseEntity findPublishById(@PathVariable("id") String publishId)  {
        return userService.findPublishById(publishId);
    }

    @GetMapping("messages/comments")
    public ResponseEntity findCommentList(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int pagesize,
                                          String messageID,
                                          String sortProp,
                                          String sortOrder){
        return userService.findCommentList(page, pagesize, messageID);
    }

    @GetMapping("videos")
    public ResponseEntity findVideoList(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize,
                                        Long uid) {
        return userService.findVideoList(page, pagesize, uid);
    }


    @PostMapping("messages/pass")
    public ResponseEntity passPublish(@RequestBody List<String> item) {
        return userService.passPublish(item);
    }

    @PostMapping("messages/reject")
    public ResponseEntity rejectPublish(@RequestBody List<String> item) {
        return userService.rejectPublish(item);
    }

    /**Mr.Seven的基操
     * POST /manage/users/freeze
     */
    @PostMapping("users/freeze")
    public ResponseEntity freezeUser(@RequestBody UserFreezeVo userFreezeVo) {
        return userFreezeService.freezeUser(userFreezeVo);
    }

    /**
     * Mr.Seven的基操
     * POST /manage/users/unfreeze
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity unfreezeUser(@RequestBody Map map) {
        Long userId = Long.valueOf(map.get("userId").toString());
        // 解封原因
        String reasonsForThawing = (String) map.get("reasonsForThawing");
        /**解封备注*/
        String frozenRemarks = (String) map.get("frozenRemarks");
        return userFreezeService.unfreezeUser(userId, reasonsForThawing, frozenRemarks);
    }

    /**
     * Mr.Seven的基操
     */
    @GetMapping("/findUserFreezeByUserId")
    public UserFreeze findUserFreezeByUserId(@RequestParam(required = false) Long userId) {
        return userFreezeService.findUserFreezeByUserId(userId);
    }

    @GetMapping("/queryFreezeByUserId")
    public ResponseEntity queryFreezeByUserId(@RequestParam(required = false) Long userId) {
        return userFreezeService.queryFreezeByUserId(userId);
    }
}
